package com.astronomy.gathering.controller;

import com.astronomy.entity.PageResult;
import com.astronomy.entity.Result;
import com.astronomy.entity.StatusCode;
import com.astronomy.gathering.pojo.Gathering;
import com.astronomy.gathering.service.GatheringService;
import com.astronomy.states.Status;
import com.github.pagehelper.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/gathering")
@CrossOrigin
public class GatheringController {


    @Autowired
    private GatheringService gatheringService;

    @GetMapping
    public Result findAll(){
        List<Gathering> gatheringList = gatheringService.findAll();
        return new Result(true, StatusCode.OK, Status.SELECT_SUCCESS,gatheringList) ;
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        Gathering gathering = gatheringService.findById(id);
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,gathering);
    }

    @PostMapping
    public Result add(@RequestBody Gathering gathering){
        gatheringService.add(gathering);
        return new Result(true,StatusCode.OK,Status.ADD_SUCCESS);
    }

    @PutMapping(value="/{id}")
    public Result update(@RequestBody Gathering gathering,@PathVariable String id){
        gathering.setId(id);
        gatheringService.update(gathering);
        return new Result(true,StatusCode.OK,Status.UPDATE_SUCCESS);
    }

    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        gatheringService.delete(id);
        return new Result(true,StatusCode.OK,Status.DELETE_SUCCESS);
    }

    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable  int page, @PathVariable  int size){
        PageResult<Gathering> pageResult = gatheringService.findPage(page, size);
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,pageResult);
    }

    @PostMapping(value = "/search" )
    public Result findList(@RequestBody Map searchMap){
        List<Gathering> list = gatheringService.findList(searchMap);
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,list);
    }

    @PostMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestBody Map searchMap, @PathVariable  int page, @PathVariable  int size){
        PageResult<Gathering> pageResult = gatheringService.findPage(searchMap, page, size);
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,pageResult);
    }

}
